package com.pigdroid.adbsqlite.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.pigdroid.adbsqlite.ADBSQLiteHelper;

public class ADBSQLiteStatement implements Statement {
	List <String> sql = new ArrayList<String>();
	private ADBSQLiteConnection connection;
	ResultSet resultSet;
	private int direction;

	public ADBSQLiteStatement(ADBSQLiteConnection con) {
		this.connection = con;
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}

	public void addBatch(String arg0) throws SQLException {
		sql.add(arg0);
	}

	public void cancel() throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public void clearBatch() throws SQLException {
		sql.clear();
	}

	public void clearWarnings() throws SQLException {

	}

	public void close() throws SQLException {

	}

	public void closeOnCompletion() throws SQLException {

	}

	public boolean execute(String arg0) throws SQLException {
		ADBSQLiteHelper helper = connection.getHelper();
		this.resultSet = new ADBSQLiteResultSet(connection, helper.executeQuery(arg0));
		return true;
	}

	public boolean execute(String arg0, int arg1) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public boolean execute(String arg0, int[] arg1) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public boolean execute(String arg0, String[] arg1) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public int[] executeBatch() throws SQLException {
		// TODO try to fetxh the stats for each query
		for (String q : sql) {
			execute(q);
		}
		return new int[]{0};
	}

	public ResultSet executeQuery(String arg0) throws SQLException {
		return new ADBSQLiteResultSet(connection, connection.getHelper().executeQuery(arg0));
	}

	public int executeUpdate(String arg0) throws SQLException {
		connection.getHelper().executeQuery(arg0);
		return 0;
	}

	public int executeUpdate(String arg0, int arg1) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public int executeUpdate(String arg0, int[] arg1) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public int executeUpdate(String arg0, String[] arg1) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public Connection getConnection() throws SQLException {
		return connection;
	}

	public int getFetchDirection() throws SQLException {
		return direction;
	}

	public int getFetchSize() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public ResultSet getGeneratedKeys() throws SQLException {
		return new ADBSQLiteResultSet();
	}

	public int getMaxFieldSize() throws SQLException {
		return Integer.MAX_VALUE;
	}

	public int getMaxRows() throws SQLException {
		return Integer.MAX_VALUE;
	}

	public boolean getMoreResults() throws SQLException {
		try {
			if (resultSet.next()) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			throw new SQLException(e);
		}
	}

	public boolean getMoreResults(int arg0) throws SQLException {
		throw new SQLFeatureNotSupportedException();
	}

	public int getQueryTimeout() throws SQLException {
		return 0;
	}

	public ResultSet getResultSet() throws SQLException {
		return resultSet;
	}

	public int getResultSetConcurrency() throws SQLException {
		return ResultSet.CONCUR_READ_ONLY;
	}

	public int getResultSetHoldability() throws SQLException {
		return ResultSet.CLOSE_CURSORS_AT_COMMIT;
	}

	public int getResultSetType() throws SQLException {
		return ResultSet.TYPE_SCROLL_INSENSITIVE;
	}

	public int getUpdateCount() throws SQLException {
		// TODO do this
		return 0;
	}

	public SQLWarning getWarnings() throws SQLException {
		return null;
	}

	public boolean isCloseOnCompletion() throws SQLException {
		return false;
	}

	public boolean isClosed() throws SQLException {
		return false;
	}

	public boolean isPoolable() throws SQLException {
		return false;
	}

	public void setCursorName(String arg0) throws SQLException {
	}

	public void setEscapeProcessing(boolean arg0) throws SQLException {
		// TODO 

	}

	public void setFetchDirection(int arg0) throws SQLException {
		direction = arg0;
	}

	public void setFetchSize(int arg0) throws SQLException {
		// fetch all
	}

	public void setMaxFieldSize(int arg0) throws SQLException {
		// no effect
	}

	public void setMaxRows(int arg0) throws SQLException {
		// no effect

	}

	public void setPoolable(boolean arg0) throws SQLException {
		// no effect

	}

	public void setQueryTimeout(int arg0) throws SQLException {
		// no effect

	}

}
